package 链表模拟实现;

import 链表模拟实现.IndexLegal;

public class MyLinkedList {
    static class ListNode{
        private int val;
        ListNode next;
        ListNode prev;

        public ListNode(int val) {
            this.val = val;
        }
    }
    //头指针
    private ListNode head;
    //尾指针
    private ListNode tail;

    //得到单链表的长度
    public int size(){
        ListNode cur=head;
        int count=0;
        while (cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    };
    public void display(){
        ListNode cur=head;
        while (cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    };
    //头插法
    public void addFirst(int data){
        ListNode node=new ListNode(data);
        if(head==null){
            head=node;
            tail=node;
        }else{
            node.next=head;
            head.prev=node;
            head=node;
        }
    };
    //尾插法
    public void addLast(int data){
        ListNode node=new ListNode(data);
        if(head==null){
            head=node;
            tail=node;
        }else{
            node.prev=tail;
            tail.next=node;
            tail=node;
        }
    };
    //判断index是否合法
    private void indexLegal(int index){
        if(index<0||index>size()-1){
            throw new IndexLegal("下标不合法");
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        ListNode node = new ListNode(data);
        if (index == 0) {
            addFirst(data);
            return true;
        } else if (index == size() - 1) {
            addLast(data);
            return true;
        }else {
            try{
                indexLegal(index);
                ListNode cur=head;
                while (index!=0){
                    cur=cur.next;
                    index--;
                }
                node.next=cur;
                node.prev=cur.prev;
                cur.prev.next=node;
                cur.prev=node;
                return true;
            }catch (IndexLegal e){
                e.printStackTrace();
            }
        }
        return false;
    };
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur=head;
        while (cur!=null){
            if(cur.val==key)return true;
            cur=cur.next;
        }
        return false;
    };
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(key==head.val){
            ListNode curNext=head.next;
            head.next=null;
            head=curNext;
            head.prev=null;
        }else if(key==tail.val){
            ListNode curPrev=tail.prev;
            tail.prev=null;
            tail=curPrev;
            tail.next=null;
        }else {
            ListNode cur=head;
            while (cur!=null){
                if(cur.val==key)break;
                cur=cur.next;
            }
            if(cur!=null) {
                cur.prev.next = cur.next;
                cur.next.prev = cur.prev;
                cur.next = null;
                cur.prev = null;
            }
        }
    };
    //删除所有值为key的节点
    public void removeAllKey(int key){
        while (key==head.val&&head.next!=null) {
            ListNode curNext = head.next;
            head.next = null;
            head = curNext;
            head.prev=null;
        }
        //其他节点是key的情况
        ListNode cur = head.next;
        while (cur!=null){
            ListNode curNext=cur.next;
            if(cur.val==key){
                if(cur.next!=null){//不是尾巴节点
                    cur.prev.next=cur.next;
                    cur.next.prev=cur.prev;
                    cur.next=null;
                    cur.prev=null;
                }else {//尾巴节点
                    ListNode curPrev=tail.prev;
                    tail.prev=null;
                    tail=curPrev;
                    tail.next=null;
                }
            }
            cur=curNext;
        }
        if(head==tail&&tail.val==key){
            head=null;
            tail=null;
        }

    };

    public void clear(){
        ListNode cur=head;
        while (cur!=null){
            ListNode curNext=cur.next;
            cur.next=null;
            cur.prev=null;
            cur=curNext;
        }
        head=null;
        tail=null;
    };
}
